package br.brpv.silvestrin.conferencia.activity.settings;

import br.brpv.silvestrin.conferencia.R;
import br.brpv.silvestrin.conferencia.helper.ShowMessage;
import br.brpv.silvestrin.conferencia.webservice.TestWebservice;
import br.brpv.silvestrin.conferencia.webservice.TestWebservice.TestWebserviceListener;
import br.brpv.silvestrin.conferencia.webservice.Webservice;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;


public class SettingServerFragment extends Fragment implements OnClickListener, TestWebserviceListener {
	
	private FragmentActivity mActivity;
	private Button saveButton;
	private EditText serverUrl;
	private EditText serverName;
	private EditText serverUser;
	private EditText serverPass;
	private TextWatcher watcher;
	
	private TestWebservice testWebservice; 

	public SettingServerFragment() {
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_setting_detail,
				container, false);

		return rootView;
	}
	
	@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);    //To change body of overridden methods use File | Settings | File Templates.
        
        mActivity = getActivity();
		
		testWebservice = new TestWebservice(mActivity, this);
		
		watcher = new TextWatcher() {
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                saveButton.setEnabled(true);
            }
            public void afterTextChanged(Editable editable) {
            }
        };

		serverUrl = (EditText)mActivity.findViewById(R.id.server_url);
		serverName = (EditText)mActivity.findViewById(R.id.server_name);
		serverUser = (EditText)mActivity.findViewById(R.id.server_user);
		serverPass = (EditText)mActivity.findViewById(R.id.server_password);
		
        loadServerParameters();

        saveButton = (Button)mActivity.findViewById(R.id.server_save);
        saveButton.setOnClickListener(this);
        
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.server_save:
	            testWebservice.validateServer(serverUrl.getText().toString());			
				break;
			default:
				break;
		}
	}
	
	private void saveServerParameters(){
        SharedPreferences preferences = mActivity.getSharedPreferences(Webservice.WEB_SERVICE_PREFERENCES, 0);
        SharedPreferences.Editor editor = preferences.edit();

        editor.putString(Webservice.SERVER_NAME, serverName.getText().toString());

        editor.putString(Webservice.SERVER_URL, serverUrl.getText().toString());

        editor.putString(Webservice.SERVER_USER, serverUser.getText().toString());

        editor.putString(Webservice.SERVER_PASSWORD, serverPass.getText().toString());

        editor.commit();

        saveButton.setEnabled(false);
    }
    private void loadServerParameters(){
        SharedPreferences preferences = mActivity.getSharedPreferences(Webservice.WEB_SERVICE_PREFERENCES, 0);

        serverName.setText(preferences.getString(Webservice.SERVER_NAME,"Silvestrin_test"));
        serverName.addTextChangedListener(watcher);

        serverUrl.setText(preferences.getString(Webservice.SERVER_URL,"http://200.174.161.8:8090/FullWSG/service.asmx"));
        serverUrl.addTextChangedListener(watcher);

        serverUser.setText(preferences.getString(Webservice.SERVER_USER, "testePdv"));
        serverUser.addTextChangedListener(watcher);

        serverPass.setText(preferences.getString(Webservice.SERVER_PASSWORD, "colsilves@."));
        serverPass.addTextChangedListener(watcher);
    }
    
    public void onValidateServer(boolean valid) {
        if(valid) {
        	testWebservice.validateUser(serverUser.getText().toString(), serverPass.getText().toString());
        }
        else {
            ShowMessage.alertDialog(mActivity, R.string.settings_server_unreachable);
        }
	}

	public void onValidateUser(boolean valid) {
        if(valid) {
            saveServerParameters();
        } else {
            ShowMessage.alertDialog(mActivity, R.string.settings_user_password_invalid);
        }
	}
	
}
